import type { UploadRawFile, UploadRequestOptions } from "element-plus";
import { post } from "@/utils/http";
export default async function useUpload<
  T extends File = UploadRawFile,
  U = boolean,
>(
  file: T,
  url: string,
  categorys?: number[],
  option?: UploadRequestOptions,
): Promise<U | undefined> {
  return new Promise((resolve, reject) => {
    let formUpload = new FormData();
    if (!file) {
      reject(false);
    }
    formUpload.append("file", file);
    if (categorys) {
      categorys.forEach((_) => {
        formUpload.append("categorys", _.toString());
      });
    }
    post({
      url,
      data: formUpload,
      headers: { "Content-Type": "multipart/form-data" },
    }).then((res) => {
      console.log(res);
      resolve(true as U);
      ElMessage({
        message: "成功了",
        type: "success",
      });
    });
  });
}
